<?php
namespace backend\controllers;


use common\models\ParkCurrent;
use common\models\ParkCurrentSpace;
use common\models\ParkSpace;
use common\models\WeiGui;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;

/**
 * Site controller
 */
class ProcessController extends Controller
{

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        $msgType="park_status";
        if($msgType == "park_status")
        {
            $this->processParkSatus();

        }
    }


    private function processParkSatus(){
        $status = "down";
        //
        if($status == "up")
        {
            $this->parkSatusUp();
        }
        else
        {
            $this->parkSatusDown();
        }

    }

    private function parkSatusUp(){
        $isWeiGui = true;
        $space_id="5dd023c2-8bc9-11e7-8d8d-005056c00008";
        if(!$isWeiGui)
        {
            //normal up
            //1. add  park current space
            $pc = new ParkCurrent([
                'id'=>\thamtech\uuid\helpers\UuidHelper::uuid(),
                'start_time'=>date()
            ]);
            $pc->save();
            //2 associate park space to park current
            $pcs = new ParkCurrentSpace([
                'id'=>\thamtech\uuid\helpers\UuidHelper::uuid(),
                'park_current_id'=>$pc->id,
                'park_space_id'=>$space_id
            ]);
            $pcs->save();

        }
        else
        {
            //weigui up
            //find assiciate park space park time
            $otherPC = null;
            $weigui = WeiGui::find()->where([
                'park_space1'=>$space_id,
                'park_space1_status'=> 0 ,
                'park_space2_status'=>1,
            ])
                ->orWhere([
                    'park_space2'=>$space_id,
                    'park_space2_status'=> 0 ,
                    'park_space1_status'=>1,
                ])
                ->one();
            if($weigui)
            {
                //other
                $otherPS = $weigui->park_space1 == $space_id
                    ? $weigui->park_space2
                    :$weigui->park_space1;

                $otherPC =  (new \yii\db\Query())
                    ->select(['start_time'=>'start_time'])
                    ->from('park_current pc')
                    ->innerJoin('park_current_space pcs')
                    ->where(['pcs.park_space_id'=> $otherPS])
                    ->one();

                $pc = new ParkCurrent([
                    'id'=>\thamtech\uuid\helpers\UuidHelper::uuid(),
                    'start_time'=>$otherPC["start_time"]
                ]);
                $pc->save();

                $pcs = new ParkCurrentSpace([
                    'id'=>\thamtech\uuid\helpers\UuidHelper::uuid(),
                    'park_current_id'=>$pc->id,
                    'park_space_id'=>$space_id
                ]);
                $pcs->save();

            }

        }
    }

    private function parkSatusDown(){
        $space_id = "3768255b-844e-4e0d-94f4-f4da585a295b";
        $weiguis = WeiGui::find()->where([
                'park_space1'=>$space_id,
                'park_space1_status' => 1
            ])
            ->orWhere([
                'park_space2'=>$space_id,
                'park_space2_status' => 1
            ])
            ->all();

        foreach ($weiguis as $wg)
        {
            if($wg->park_space1 == $space_id)
            {
                $wg->park_space1_status = 0;
                if($wg->park_space2_status == 0)
                    $wg->car_status = 0;
                $wg->save();
            }
            else if($wg->park_space2 == $space_id)
            {
                $wg->park_space2_status = 0;
                if($wg->park_space1_status == 0)
                    $wg->car_status = 0;
                $wg->save();
            }
        }

    }

}
